package com.cq.hd.order.api.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.cq.hd.common.serialize.Decimal2Serializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class AdminOrderDetailVo implements Serializable {

    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "商家id")
    private Long businessId;

    @ApiModelProperty(value = "活动id")
    private Long activeId;

    @ApiModelProperty(value = "是否是零元购活动（1-不是，2-是）")
    private Integer zeroState;

    @ApiModelProperty(value = "一级经纪人id")
    private Long agentId;

    @ApiModelProperty(value = "二级经纪人id")
    private Long agent2Id;

    @ApiModelProperty(value = "下单用户id")
    private Long userId;

    @ApiModelProperty(value = "订单号")
    private String orderNo;

    @ApiModelProperty(value = "订单类型(1-团购订单,2-报名订单,4-团购+报名订单,5积分订单)")
    private Integer orderType;

    @ApiModelProperty(value = "商家自有商户id")
    private Long merchantId;

    @ApiModelProperty(value = "活动标题")
    private String activeTitle;

    @ApiModelProperty(value = "活动图片")
    private String activeImg;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "活动开始时间")
    private LocalDateTime startTime;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "活动结束时间")
    private LocalDateTime endTime;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "活动有效开始时间")
    private LocalDateTime validStartTime;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "活动有效结束时间")
    private LocalDateTime validEndTime;

    @ApiModelProperty(value = "补贴类型(0-未设置,1-自动按比例分摊,2-手动设置)")
    private Integer subsidyState;

    @ApiModelProperty(value = "多件多折状态(1-关闭，2-开启)")
    private Integer discountState;

    @ApiModelProperty(value = "是否是阶梯价格(1-否，2-是)")
    private Integer stepPriceState;

    @ApiModelProperty(value = "分销开启状态(1-关闭，2-开启)")
    private Integer agentState;

    @ApiModelProperty(value = "核销方式(1-线上核销，2-线下核销)")
    private Integer verifyMethod;

    @ApiModelProperty(value = "未满员是否自动退款:1-是，2-否")
    private Integer noFullAutoRefundState;

    @ApiModelProperty(value = "活动总库存")
    private Integer totalStock;

    @ApiModelProperty(value = "订单状态(1-待支付,2-待核销,3-核销中,4-已取消,5-已完成,6-已关闭)")
    private Integer orderState;

    @ApiModelProperty(value = "下单数量")
    private Integer orderNum;


    @ApiModelProperty(value = "活动价格(如果是报名活动的阶梯价格，则是阶梯价格的最大价格)")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal activePrice;


    @ApiModelProperty(value = "订单总额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal orderPrice;


    @ApiModelProperty(value = "实付金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal payPrice;

    @ApiModelProperty(value = "红包流水id")
    private Long redPacketBillId;


    @ApiModelProperty(value = "红包金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal redPacketAmt;


    @ApiModelProperty(value = "优惠金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal discountAmt;

    @ApiModelProperty(value = "其他权益")
    private String otherEquity;

    @ApiModelProperty(value = "计算佣金类型(0-无,1-固定佣金,2-专属佣金比例)")
    private Integer calCommissionType;


    @ApiModelProperty(value = "实际全核销佣金金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal actualCommissionAmt;

    @ApiModelProperty(value = "实际全核销佣金金额(二级经纪人)")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal subAgentCommissionAmt;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "支付时间")
    private LocalDateTime payTime;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "下单时间")
    private LocalDateTime orderTime;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "订单取消时间")
    private LocalDateTime cancelTime;

    @ApiModelProperty(value = "订单取消原因")
    private String cancelReason;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "订单完成时间(核销时间)")
    private LocalDateTime finishTime;

    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    @ApiModelProperty(value = "订单关闭时间")
    private LocalDateTime closeTime;

    @ApiModelProperty(value = "订单备注")
    private String orderNote;


    @ApiModelProperty(value = "下单人昵称")
    private String orderNickName;

    @ApiModelProperty(value = "下单人手机号")
    private String orderMobile;

    @ApiModelProperty(value = "一级经纪人昵称")
    private String agentNickName;

    @ApiModelProperty(value = "一级经纪人手机号")
    private String agentMobile;

    @ApiModelProperty(value = "二级经纪人昵称")
    private String agent2NickName;

    @ApiModelProperty(value = "二级经纪人手机号")
    private String agent2Mobile;

    @ApiModelProperty(value = "所属商家名称")
    private String businessName;


    @ApiModelProperty(value = "推广权益类型:0-无,1-其他权益,2-佣金 如果是其他权益,则取otherEquity字段数据,如果是佣金,则取actualCommissionAmt实际全核销佣金金额和totalCommissionAmt总实际佣金")
    private Integer promotionEquityType;


    @ApiModelProperty(value = "总实际佣金(订单核销后计算实际到账佣金)")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal totalCommissionAmt;


    @ApiModelProperty(value = "总补贴金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal totalSubsidyAmt;


    @ApiModelProperty(value = "总返还金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal totalReturnAmt;

    @ApiModelProperty(value = "子订单列表")
    private List<AdminOrderItemVo> adminOrderItems;

    @ApiModelProperty(value = "合计分账本金(按比例分配的用户支付的订单款)")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal totalMerchantAmt;

    @ApiModelProperty(value = "一级经纪人可得佣金")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal agent1CommissionAmt;

    @ApiModelProperty(value = "二级经纪人可得佣金")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal agent2CommissionAmt;

    @ApiModelProperty(value = "合计分账补贴(该订单商场补贴的金额)")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal totalMerchantSubsidyAmt;

    @ApiModelProperty(value = "是否包含票付通")
    private Boolean isPft;

    @ApiModelProperty(value = "报名信息")
    private List<OrderEnrollInfoVo> orderEnrollInfos;

    @ApiModelProperty(value = "已报名人数")
    private Integer enrolledNum;

    @ApiModelProperty(value = "一级经纪人可得积分")
    private BigDecimal agentPointsAmt;

    @ApiModelProperty(value = "二级经纪人可得积分")
    private BigDecimal subAgentPointsAmt;

    @ApiModelProperty(value = "GROUP_BUY(拼团)")
    private String promotionType;

    @ApiModelProperty(value = "是否免单：0-否 1-是")
    private Integer free;

    @ApiModelProperty(value = "子单分组")
    private List<AdminOrderItemGroupVo> itemGroups;

    public List<AdminOrderItemGroupVo> getItemGroups() {
        Map<Integer, List<AdminOrderItemVo>> itemGroups = adminOrderItems.stream()
                .collect(Collectors.groupingBy(AdminOrderItemVo::getItemGroup));
        List<AdminOrderItemGroupVo> resultList = new ArrayList<>();
        for (Map.Entry<Integer, List<AdminOrderItemVo>> itemGroup : itemGroups.entrySet()) {
            AdminOrderItemGroupVo adminOrderItemGroupVo = new AdminOrderItemGroupVo();
            adminOrderItemGroupVo.setItemGroup(itemGroup.getKey());
            adminOrderItemGroupVo.setOrderItems(itemGroup.getValue());
            resultList.add(adminOrderItemGroupVo);
        }
        resultList.sort(Comparator.comparingInt(AdminOrderItemGroupVo::getItemGroup));

        return resultList;
    }


}
